<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/5 0005
 * Time: 11:28
 */

namespace app\common\widget;

use Exception;

/**
 * Class WidgetInput
 * @package app\common\widget
 * @method $this setWidthColXs($value) 设置Xs(min-width:0px)宽度值,$value=1到12
 * @method $this setWidthColSm($value) 设置Sm(min-width:768px)宽度值,$value=1到12
 * @method $this setWidthColMd($value) 设置Md(min-width:992px)宽度值,$value=1到12
 * @method $this setWidthColLg($value) 设置Lg(min-width:1200px)宽度值,$value=1到12
 */
class WidgetInput extends WidgetCommon
{

    /**
     *  默认class属性值
     */
    const defaultClass = 'form-control';

    /**
     * 文本类型
     */
    const inputTypeText = "text";

    /**
     * radio类型
     */
    const inputTypeRadio = "radio";

    /**
     * checkbox类型
     */
    const inputTypeCheckBox = "checkbox";

    /**
     * hidden表单
     */
    const inputTypeHidden = "hidden";

    /**
     * bootstrap col 宽度列表默认值
     * @var array
     */
    private $widthList = array(
            'xs' => WidgetConst::colxs12,
            'sm' => WidgetConst::colsm12,
            'md' => WidgetConst::colmd12,
            'lg' => WidgetConst::collg12,
    );

    /**
     * 渲染挂件
     * @return mixed
     * @throws \think\Exception
     * @throws Exception
     */
    function render()
    {
        $id = $this->getId();
        if ( !$id ) $this->throwException("请设置表单元素ID");
        return $this->fetch();
    }

    /**
     * 初始化方法
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认class
        $this->setClass(self::defaultClass);
        // 默认为文本类型
        $this->setInputType(self::inputTypeText);
    }

    /**
     * 设置Input的type类型
     * @param $inputType
     * @return $this
     */
    protected function setInputType( $inputType ) {
        $this->setDomAttr( 'type', $inputType );
        return $this;
    }

    /**
     * 设置隐藏表单
     * @return $this
     */
    public function setHidden() {
        $this->setInputType(self::inputTypeHidden);
        return $this;
    }

    /**
     * 获取Input的type类型
     * @return mixed|null
     */
    public function getInputType() {
        return $this->getDomAttr('type');
    }

    /**
     * 设置Input的name属性值
     * @param $value
     * @return $this
     * @throws Exception
     */
    public function setName( $value ) {
        if ( !$value ) $this->throwException("Input的name属性非法");
        $this->setDomAttr( 'name', $value );
        return $this;
    }

    /**
     * 获取Input的name属性值
     * @return mixed|null
     */
    public function getName() {
        return $this->getDomAttr('name');
    }

    /**
     * 设置表单value值
     * @param $value
     * @return $this
     */
    public function setValue( $value ) {
        $this->setDomAttr( 'value', $value );
        return $this;
    }

    /**
     * 获取表单value值
     * @return mixed|null
     */
    public function getValue() {
        return $this->getDomAttr('value');
    }

    /**
     * 设置表单标题
     * @param $title
     * @return $this
     */
    public function setTitle($title)  {
        $this->setDomAttr( 'title', $title );
        return $this;
    }


    /**
     * 获取表单标题
     * @return mixed|null
     */
    public function getTitle() {
        return $this->getDomAttr('title');
    }

    /**
     * 设置media宽度值
     * @param $media
     * @param $value
     * @return $this
     */
    private function setWidth( $media, $value ) {
        $this->widthList[$media] = $value;
        return $this;
    }

    /**
     * 返回Col宽度列表
     * @return array
     */
    public function getWidth() {
        return $this->widthList;
    }

    public function setRequired() {}

    public function setReadOnly() {}

    public function setDisabled() {}

    public function setMaxLength() {}

    public function setMinLength() {}

    /**
     * __call 魔术方法
     * @param $name
     * @param $arguments
     * @return $this|WidgetInput
     * @throws \think\Exception
     */
    public function __call($name, $arguments)
    {
        // 匹配宽度
        if ( strpos( $name, 'setWidthCol' ) !==false ) {
            $style = str_replace( "setWidth", "", $name );
            if ( $arguments[0]<1 || $arguments[0]>12 ) $this->throwException( "编辑表单Input元素阈值错误" );
            $const = strtolower($style).$arguments[0];
            $width = WidgetConst::getConstant($const);
            // 匹配出media
            $pattern = "/^col([A-Za-z]*)[1-9]*/";
            preg_match($pattern,$const,$match);
            $media = $match[1];
            return $this->setWidth( $media, $width );
        }
        return $this;
    }


}